#include<bits/stdc++.h>
#define N 200010
using namespace std;
int n,m,k,i,x,y,h[N],t;
long long ans,Ans,g[N],f[N];
struct mjj{int x,y,z;}a[N];
bool cmp(mjj a,mjj b){return a.x<b.x;}
int main() {
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=m;i++){
scanf("%d%d%d%d",&a[i].x,&x,&y,&a[i].z);
if(x==0)a[i].y=y;
else a[i].y=-x;
}
Ans=0;
Ans=1e18;
sort(a+1,a+m+1,cmp);
for(i=1;i<=m;i++)h[i]=a[i].x;
for(i=1;i<=n;i++)f[i]=1e12,ans+=f[i];
g[0]=1e18;
for(i=1;i<=m;i++)if(a[i].y<0){
ans-=f[-a[i].y];
f[-a[i].y]=min(f[-a[i].y],1ll*a[i].z);
ans+=f[-a[i].y];
g[i]=ans;
}else g[i]=g[i-1];
memset(f,63,sizeof(f));
ans=0;
for(i=1;i<=n;i++)f[i]=1e12,ans+=f[i];
for(i=m;i>=1;i--)if(a[i].y>0){
ans-=f[a[i].y];
f[a[i].y]=min(f[a[i].y],1ll*a[i].z);
ans+=f[a[i].y];
t=lower_bound(h+1,h+m+1,a[i].x-k)-h-1;
if(t)Ans=min(Ans,ans+g[t]);
}
if(Ans>=1e12)puts("-1");
else printf("%I64d\n",Ans);
}
165B - Burning Midnight Oil | 17A - Noldbach problem |
1350A - Orac and Factors | 1373A - Donut Shops |
26A - Almost Prime | 1656E - Equal Tree Sums |
1656B - Subtract Operation | 1656A - Good Pairs |
1367A - Short Substrings | 87A - Trains |
664A - Complicated GCD | 1635D - Infinite Set |
1462A - Favorite Sequence | 1445B - Elimination |
1656C - Make Equal With Mod | 567A - Lineland Mail |
1553A - Digits Sum | 1359B - New Theatre Square |
766A - Mahmoud and Longest Uncommon Subsequence | 701B - Cells Not Under Attack |
702A - Maximum Increase | 1656D - K-good |
1426A - Floor Number | 876A - Trip For Meal |
1326B - Maximums | 1635C - Differential Sorting |
961A - Tetris | 1635B - Avoid Local Maximums |
20A - BerOS file system | 1637A - Sorting Parts |